grammar minisyys;
file: (globalVar|functionDefinition)* EOF;

globalVar: CONST? type ass(',' ass)* ';';

functionDefinition:
	type ID '(' name? ')' functionBody;

name: type ID (',' type ID)*;

type: VOID | INT;

functionBody: '{' statement* '}';
compoundstatement: '{' statement* '}';
statement:
	compoundstatement
	| selectionstatement
	| whilestatement
	| jumpstatement
	| retstatement ';'
	| functionCall ';'
	| declarationarId ';'
	| declarationId ';'
	| ID';'
	 ;


selectionstatement: IF '(' expression ')' statement  (ELSE statement)?;
whilestatement: WHILE '(' expression ')'statement;
jumpstatement: BREAK ';' | CONTINUE ';';

retstatement:RETURN expression?;

functionCall: ID  '(' expressionList? ')';
expressionList: expression (',' expression)*;

expression: andExpr ('||' andExpr)*;
andExpr: eqExpr ('&&' eqExpr)*;
eqExpr: relExpr (('==' | '!=') relExpr)*;
relExpr: addExpr (('>=' | '<=' | '>' | '<') addExpr)*;
addExpr: mulExpr (('+' | '-') mulExpr)*;
mulExpr: opExpr (('*' | '/' | '%') opExpr)*;
opExpr: unop* atom;
unop :'-'|'!'|'+';
atom:    INTP | lookup;
lookup
	: functionCall index?
  |  '(' expression ')' index?
  |  ID index? ;

index: '[' expression ']';




declarationarId: CONST? type? arr(','arr)*;
declarationId: CONST? type?  ass (',' ass)*;

arr:ID index+ ('=' iniList)?;
iniList:'{' ini (',' ini)* '}';
ini: expression;
ass:ID ('=' expression)?;


CONST: 'const';

VOID: 'void';
INT: 'int';

WHILE: 'while';
BREAK: 'break';
CONTINUE: 'continue';

RETURN: 'return';
IF: 'if';
ELSE: 'else';

LPAREN: '(';
RPAREN: ')';
LBRACE: '{';
RBRACE: '}';
LBRACKET: '[';
RBRACKET: ']';
ADD: '+';
SUB: '-';
MUL: '*';
DIV: '/';
MODULUS: '%';
NUMBER: [0-9]+ | [0-9]+ '.' [0-9]* | [0-9]* '.' [0-9]+;


EQUALS: '==';
NEQUALS: '!=';
MORETHAN: '>';
LESSTHAN: '<';
GTEQUALS: '>=';
LTEQUALS: '<=';
OR: '||';
AND: '&&';

SCOLON: ';';
ASSIGN: '=';
COMMA: ',';
QMARK: '?';
COLON: ':';
EXCL: '!';

ID:[a-zA-Z_][a-zA-Z_0-9]*;

INTP:DIGITS;
HEX:'0x'|'0X';
OCT:'0';
DIGITS:HEX? [0-9a-fA-F]+;
COMMENT: ('//' ~[\r\n]* | '/*' .*? '*/') -> skip;
SPACE: [ \t\r\n\u000C] -> channel(HIDDEN);
